﻿//************************************
// @FileName: MainWindow.xaml.cs
// @Brief   : 服务器端
// @Author  : Waterstrong
// @DateTime: 2012-11-7 9:28:18
//************************************
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Net;
using System.Net.Sockets;
using System.Threading;

namespace Server
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        private Thread _listenThread = null; // 线程对象
        private TcpListener _listener = null;
        
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            IPAddress[] ipList = Dns.GetHostAddresses(Dns.GetHostName());
            foreach (IPAddress ip in ipList)
            {
                if (ip.AddressFamily == AddressFamily.InterNetwork)
                {
                    rtb_msg.AppendText("[" + ip.ToString() + "]Server is running ...\n");
                    _listener = new TcpListener(ip, 8500);
                    _listener.Start();           // 开始侦听
                    rtb_msg.AppendText("Start listening ...\n");
                    break;
                }
            }
            //开一个线程
            _listenThread = new Thread(new ThreadStart(ReceiveMsg));
            _listenThread.Start();
        }

        private void rtb_msg_TextChanged(object sender, TextChangedEventArgs e)
        {

        }

        private void ReceiveMsg()
        {
            
            while (_listener != null)
            {
                // 获取一个连接，中断方法
                TcpClient client = _listener.AcceptTcpClient();
                // 打印连接到的客户端信息
                rtb_msg.Dispatcher.Invoke(new Action(() =>
                {
                    rtb_msg.AppendText("Client Connected！" + client.Client.LocalEndPoint.ToString() + "-->" + client.Client.RemoteEndPoint.ToString() + "\n");
                }));
                //MessageBox.Show("Client Connected！" + client.Client.LocalEndPoint.ToString() + "-->" + client.Client.RemoteEndPoint.ToString() + "\n");
                RemoteClient wapper = new RemoteClient(client, this);
            }
        }

        private void btn_stop_Click(object sender, RoutedEventArgs e)
        {
            StopServer();
        }

        private void StopServer()
        {
            if (_listener != null)
            { 
                _listenThread.Abort();
                _listener.Stop();
                _listener = null;
                rtb_msg.AppendText("服务器已停止运行！");
            }
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            StopServer();
        }
    }
}
